package multiThreaded;

import java.util.Date;
import java.util.Map;

import singleThreaded.DataGatherer;
import singleThreaded.Reporter;

/**
 * Data gatherer thread for the web spider.
 * @author DoubleS
 * @version 1.0
 */
public class DataGathererThread extends DataGatherer implements Runnable
{
  /**
   * The whole text of this web page.
   */
  private String my_text;
  
  /**
   * The starting time of this web spider.
   */
  private final double my_start_time;
  
  /**
   * The current link.
   */
  private String my_current_link;
  
  /**
   * The max number of pages that need to be crawled.
   */
  private final int my_limit;
  
  /**
   * The current total number of links (both visited and unvisited).
   */
  private int my_current_total_links;
  
  /**
   * The number of pages that have been parsed.
   */
  private int my_num_pages; 
  /**
   * my_reporter;
   */
  private ReporterThread my_reporter;
  
  /**
   * Construct a DataGathererThread with the initial inputs.
   * @param the_keywords The keywords.
   * @param the_start_time The starting time of this web spider.
   * @param the_limit The max number of pages that need to be crawled.
   */
  public DataGathererThread(final String[] the_keywords, final double the_start_time,
                            final int the_limit)
  {
    //my_data_gatherer = new DataGatherer(the_keywords);
    super(the_keywords);
    my_start_time = the_start_time;
    my_limit = the_limit;
    my_text = "";
    my_current_link = "";
    my_current_total_links = 0;
    my_num_pages = 0;
  }
  
  /**
   * Feed the data gatherer with new inputs.
   * @param the_text The text of the web page.
   * @param the_link The link of the web page.
   * @param the_current_total_links The current total retrieved links.
   * @param the_counter The counter for the number of parsed pages.
   */
  public void updateDataGatherer(final String the_text, final String the_link,
                                final int the_current_total_links, final int the_counter)
  {
    my_text = the_text;
    my_current_link = the_link;
    my_current_total_links = the_current_total_links;
    my_num_pages = the_counter;
  }
  
  /**
   * Run the data gatherer and create the thread reporter.
   */
  public void run()
  {
    //System.out.println("I'm in the Data Gatherer Thread");
    processWords(my_text);
    final Date finishDate = new Date();
    final double time_elapsed = finishDate.getTime() - my_start_time;
    final Map<String, Integer> result = this.getMapKeywordHit();
    my_reporter =
        new ReporterThread(result.entrySet(), my_num_pages, this.getWordsNumber(),
                           my_current_total_links, my_current_link, my_limit, time_elapsed);
    final Thread reporter_thread = new Thread(my_reporter);
    reporter_thread.start();
    //my_reporter.MultiThreadTextReport();
    /*
    try
    {
      reporter_thread.join();
    }
    catch (final InterruptedException e)
    {
      e.printStackTrace();
    }*/
  }
  /**
   *
   */
   public Reporter getReporter()
   {
     return my_reporter;
   }

}
